const connection = require('../app/database')
class UserService {
    async create(user) {
        // console.log(user)
        // 1.获取用户user
        const { name, password } = user
        // 2.拼接statement
        const statement = 'INSERT INTO `user` (name,password) VALUES (?,?);'
        // 3.执行sql语句
        const [result] = await connection.execute(statement, [name, password])
        return result

    }
    async findUserByName(name) {
        const statement = 'SELECT * FROM `user` WHERE name=?;'
        const [result] = await connection.execute(statement, [name])
        return result
    }
    async findAvatarById(userId) {
        const statement = 'SELECT * FROM `avatar` WHERE user_id=?;'
        const [result] = await connection.execute(statement, [userId])
        return result.pop()
    }
    async updateUserAvatar(avatarUrl, id) {
        const statement = 'UPDATE user SET avatar_url = ? WHERE id = ?'
        const [result] = await connection.execute(statement, [avatarUrl, id])
        return result
    }
}
module.exports = new UserService()